package com.hndd.smartfactoryapp.activity;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public abstract class BaseActivity extends AppCompatActivity {
    private static final String TAG = "BaseActivity";
    protected Context context;
    protected Gson gson;

    @Override
    protected final void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setView());
        gson = new Gson();
        context = getApplicationContext();
        run();
    }

    //预留的运行代码的方法
    protected void run() {

    }

    protected abstract int setView();

    protected abstract void initView();

    protected abstract void initData();

    /**
     * 这里写上需要在新线程运行的方法
     */
    protected void runOnNewThread() {

    }

    /**
     * 将开启一条新线程,该线程会运行runOnNewThread()中的语句
     */
    protected final void startThread() {
       new Thread() {
           @Override
           public void run() {
               super.run();
               runOnNewThread();
           }
       }.start();
    }

    /**
     * 发送一条带json参数的post请求,需在新线程调用
     * @param reqBody 请求体
     * @param url 地址
     * @return 返回服务器返回的json数据
     */
    protected final JSONObject getResponse(JSONObject reqBody,String url) {
        JSONObject response = null;
        MediaType mediaType = MediaType.parse("application/json;charset=utf8");
        RequestBody body = RequestBody.create(mediaType, reqBody.toString());

        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .post(body)
                .url(url)
                .build();

        Call call = okHttpClient.newCall(request);
        try {
            Response execute = call.execute();
            String string = execute.body().string();
            response = new JSONObject(string);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return response;
        }
    }

    /**
     * 发送一条不带参数的请求,需在新线程调用
     * @param url 请求地址
     * @return 返回服务器返回的数据
     */
    protected final JSONObject getResponse(String url) {
        JSONObject response = null;

        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();

        Call call = okHttpClient.newCall(request);
        try {
            Response execute = call.execute();
            String string = execute.body().string();
            response = new JSONObject(string);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return response;
        }
    }
}
